package com.ruoyi.llm.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.llm.dao.KnowledgeInfoMapper;
import com.ruoyi.llm.domain.po.KnowledgeDocInfoPo;
import com.ruoyi.llm.domain.po.KnowledgeInfoPo;
import com.ruoyi.llm.domain.vo.KnowledgeDocInfoVo;
import com.ruoyi.llm.domain.vo.KnowledgeInfoVo;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@DataSource(DataSourceType.MASTER)
public class KnowledgeInfoService  extends ServiceImpl<KnowledgeInfoMapper, KnowledgeInfoPo> {


    @Autowired
    private KnowledgeDocInfoService knowledgeDocInfoService;

    @Autowired
    private KnowledgeParagraphService knowledgeParagraphService;

    public KnowledgeInfoVo getKnowledgeInfo(String docId){
        KnowledgeInfoPo ret=this.getById(docId);
        List<KnowledgeDocInfoPo> docs=knowledgeDocInfoService.knowledgeDocList(ret.id);
        List<KnowledgeDocInfoVo> docViews = Lists.newArrayList();
        KnowledgeInfoVo knowledge= new KnowledgeInfoVo();
        BeanUtils.copyProperties(ret,knowledge);
        for(KnowledgeDocInfoPo doc:docs){
            KnowledgeDocInfoVo docView = new KnowledgeDocInfoVo();
            BeanUtils.copyProperties(doc,docView);
            docView.setParaCount( knowledgeParagraphService.getParagraphCount(doc.getDocId()));
            docViews.add(docView);
        }

        knowledge.setDocs(docViews);
        return knowledge;
    }

    public KnowledgeInfoPo getOneKnowledgeByproject(String projectId){
        LambdaQueryWrapper<KnowledgeInfoPo> query=new LambdaQueryWrapper<>();
        query.eq(KnowledgeInfoPo::getProjectId,projectId);
        return this.getOne(query);
    }

    public List<KnowledgeInfoPo> listKnowledgeInfo(String name){
        LambdaQueryWrapper<KnowledgeInfoPo> query=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            query.like(KnowledgeInfoPo::getName,name);
        }
        List<KnowledgeInfoPo> ret=this.list(query);
        for(KnowledgeInfoPo po:ret){
            List<KnowledgeDocInfoPo> docs=knowledgeDocInfoService.knowledgeDocList(po.id);
            po.setDocs(docs);
        }
        return ret;
    }

    public String createKnowlege(KnowledgeInfoPo kledgeInfo){
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getName())){
            return "请输入知识库名称";
        }
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getDescript())){
            return "请输入知识库描述";
        }
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getProjectId())){
            return "请输入所属应用";
        }
        LambdaQueryWrapper<KnowledgeInfoPo> query=new LambdaQueryWrapper<>();
        query.eq(KnowledgeInfoPo::getProjectId,kledgeInfo.getProjectId());
        List<KnowledgeInfoPo> ret=this.list(query);
        if(!ret.isEmpty()){
            return  "该应用已经绑定知识库";
        }else{
            kledgeInfo.setCreator("管理员");
            // 创建一个Timestamp对象
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            kledgeInfo.setCreateTime(timestamp);
            kledgeInfo.setUpdateTime(timestamp);
            this.save(kledgeInfo);
            return "保存成功";
        }
    }


    public String updateKnowlege(KnowledgeInfoPo kledgeInfo){
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getId())){
            return "请选择要修改的知识库";
        }
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getName())){
            return "请输入知识库名称";
        }
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getDescript())){
            return "请输入知识库描述";
        }
        if(kledgeInfo == null || StringUtils.isBlank(kledgeInfo.getProjectId())){
            return "请输入所属应用";
        }
        LambdaQueryWrapper<KnowledgeInfoPo> query=new LambdaQueryWrapper<>();
        query.eq(KnowledgeInfoPo::getProjectId,kledgeInfo.getProjectId())
                .ne(KnowledgeInfoPo::getId,kledgeInfo.getId());
        List<KnowledgeInfoPo> ret=this.list(query);
        if(!ret.isEmpty()){
            return  "该应用已绑定知识库";
        }else{
            // 创建一个Timestamp对象
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            kledgeInfo.setUpdateTime(timestamp);
            this.updateById(kledgeInfo);
            return "保存成功";
        }
    }

}
